package cn.itcast.controller;

import cn.itcast.domain.Standard;
import cn.itcast.service.StandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StandardController {
    /**
     * 注入service层
     */
    @Autowired
    private StandService standService;


    //添加的方法
    @RequestMapping("/standard_save")
    public String add(Standard standard){
        standService.add(standard);//调用service添加的方法
        return "redirect:/pages/base/standard.html";//重定向
    }

    //分页查询
    @RequestMapping("/standard_pageQuery")
    @ResponseBody
    public Map pageQuery(int page, int rows){
        Page<Standard> standards = standService.pageQuery(page, rows);
        HashMap<String,Object> result = new HashMap<>();
        result.put("total",standards.getTotalElements());//standards.getTotalElements总的记录数
        result.put("rows",standards.getContent());//standards.getContent()获取内容
        return result;
    }

    //查询所有
    @RequestMapping("/standard_findAll")
    @ResponseBody
    public List findAll(){
        return standService.findAll();
    }
}
